package com.charity.service.serviceImpl;

import com.charity.mapper.EntryMapper;
import com.charity.model.CommonResponse;
import com.charity.model.bo.*;
import com.charity.pojo.Donation;
import com.charity.pojo.Entry;
import com.charity.service.CharityService;
import com.charity.service.EntryService;
import com.charity.service.ServiceManager;
import org.fisco.bcos.sdk.transaction.model.dto.TransactionResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ClassUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.LinkOption;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class EntryServiceImpl implements EntryService {

    @Autowired
    private EntryMapper entryMapper;

    @Autowired
    private CharityService charityService;

    @Autowired
    private ServiceManager serviceManager;

    @Override
    public List<Entry> entryByFbu() {
        return entryMapper.entryByFbu();
    }

    @Override
    public Entry entryById(Long id) {
        return entryMapper.entryById(id);
    }

    @Override
    public Integer addEntry(Long id, String entryName, Long recipientId, String introduction, String Etype, Long targetAmount, String beginDate, String endDate,
                            String account, String state, Integer days, MultipartFile file) throws Exception {
        charityService.init();
//        String staticPath = ClassUtils.getDefaultClassLoader().getResource("static").getPath();
//        String fileName = file.getOriginalFilename();  //获取文件名
//
//        // 图片存储目录及图片名称
//        String url_path = "images" + File.separator + fileName;
//        //图片保存路径
//        String savePath = staticPath + File.separator + url_path;
//        // 访问路径=静态资源路径+文件目录路径
        String visitPath ="http:localhost:8080/static/" ;//+ url_path;
//        File saveFile = new File(savePath);
//        if (!saveFile.exists()){
//            saveFile.mkdirs();
//        }
//        try {
//            file.transferTo(saveFile);  //将临时存储的文件移动到真实存储路径下
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        serviceManager.init();
        Map<String, CharityService> map = serviceManager.initCharityServiceManager();
        List<String> k = new ArrayList<>();
        for (String key : map.keySet()) {
            k.add(key);
        }
        charityService = map.get(k.get(0));
        CharityAddEntrydInputBO AddEntry_dInputBO = new CharityAddEntrydInputBO(id, entryName, recipientId, introduction, Etype,
                visitPath, targetAmount, new Date().getTime(), new Date().getTime()+(days*24*3600*1000), account);
        charityService.addEntryd(AddEntry_dInputBO);
        Integer result = entryMapper.addEntry(id, entryName, recipientId, introduction, Etype, visitPath, targetAmount, beginDate, endDate, account, state);
        if (result>0) {
            return result;
        }
        return 0;
    }

    @Override
    public Long entryByType() {
        return entryMapper.entryByType();
    }

    @Override
    public Integer updEntryCol(Long mount, Long id) {
        return entryMapper.updEntryCol(mount, id);
    }

    @Override
    public List<Entry> entryByRecId(Long recipientId) {
        return entryMapper.entryByRecId(recipientId);
    }

    @Override
    public Integer updEntry(Long id, String introduction, String Etype, Long targetAmount, String state, String reviewer,MultipartFile file) throws Exception {
//        String staticPath = ClassUtils.getDefaultClassLoader().getResource("static").getPath();
//        String fileName = file.getOriginalFilename();  //获取文件名
//
//        // 图片存储目录及图片名称
//        String url_path = "images" + File.separator + fileName;
//        //图片保存路径
//        String savePath = staticPath + File.separator + url_path;
        // 访问路径=静态资源路径+文件目录路径
        String visitPath ="http:localhost:8080/static/";// + url_path;
//        File saveFile = new File(savePath);
//        if (!saveFile.exists()){
//            saveFile.mkdirs();
//        }
//        try {
//            file.transferTo(saveFile);  //将临时存储的文件移动到真实存储路径下
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        serviceManager.init();
        Map<String, CharityService> map = serviceManager.initCharityServiceManager();
        List<String> k = new ArrayList<>();
        for (String key : map.keySet()) {
            k.add(key);
        }
        charityService = map.get(k.get(0));
        charityService.init();
        CharityUpdateEntry_dInputBO charityUpdateEntry_dInputBO = new CharityUpdateEntry_dInputBO(id, introduction, Etype, visitPath, targetAmount, state, reviewer);
//        charityService.updateEntry_d(charityUpdateEntry_dInputBO);
        return entryMapper.updEntry(id, introduction, Etype, visitPath, targetAmount, state, reviewer);
    }

    @Override
    public Integer updEntryBySQ(Long id,String account, String stateM) {
        return entryMapper.updEntryBySQ(id, account, stateM);
    }

    @Override
    public Integer updEntryByBC(Long id, String whereAbouts, MultipartFile file) throws Exception {
//        String staticPath = ClassUtils.getDefaultClassLoader().getResource("static").getPath();
//        String fileName = file.getOriginalFilename();  //获取文件名
//
//        // 图片存储目录及图片名称
//        String url_path = "images" + File.separator + fileName;
//        //图片保存路径
//        String savePath = staticPath + File.separator + url_path;
        // 访问路径=静态资源路径+文件目录路径
        String visitPath ="http:localhost:8080/static/";// + url_path;
//        File saveFile = new File(savePath);
//        if (!saveFile.exists()){
//            saveFile.mkdirs();
//        }
//        try {
//            file.transferTo(saveFile);  //将临时存储的文件移动到真实存储路径下
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        serviceManager.init();
        Map<String, CharityService> map = serviceManager.initCharityServiceManager();
        List<String> k = new ArrayList<>();
        for (String key : map.keySet()) {
            k.add(key);
        }
        charityService = map.get(k.get(0));
        charityService.init();
        CharityUpdateEntryTobuchongInputBO updateEntryTobuchongInputBO = new CharityUpdateEntryTobuchongInputBO(id, whereAbouts ,visitPath);
//        charityService.updateEntryTobuchong(updateEntryTobuchongInputBO);
        return entryMapper.updEntryByBC(id, whereAbouts, visitPath);
    }

    @Override
    public List<Entry> selBySH() {
        return entryMapper.selBySH();
    }

    @Override
    public Integer updSh(Entry entry) throws Exception {
//        serviceManager.init();
//        Map<String, CharityService> map = serviceManager.initCharityServiceManager();
//        List<String> k = new ArrayList<>();
//        for (String key : map.keySet()) {
//            k.add(key);
//        }
//        charityService = map.get(k.get(0));
//        charityService.init();
        CharityUpdateEntry_dInputBO charityUpdateEntry_dInputBO = new CharityUpdateEntry_dInputBO(entry.getId(), entry.getIntroduction(), entry.getType(), entry.getPic(),
                entry.getTargetAmount(),"通过", entry.getReviewer());
//        charityService.updateEntry_d(charityUpdateEntry_dInputBO);
        return entryMapper.updSh(entry.getId(), "通过", entry.getReviewer());
    }

    @Override
    public List<Entry> selWTG() {
        return entryMapper.selWTG();
    }

    @Override
    public Integer delD(Long id) throws Exception {
        serviceManager.init();
        Map<String, CharityService> map = serviceManager.initCharityServiceManager();
        List<String> k = new ArrayList<>();
        for (String key : map.keySet()) {
            k.add(key);
        }
        charityService = map.get(k.get(0));
        charityService.init();
        CharityDeleteEntry_dInputBO deleteEntry_dInputBO = new CharityDeleteEntry_dInputBO(id);
        charityService.deleteEntry_d(deleteEntry_dInputBO);
        return entryMapper.delD(id);
    }

    @Override
    public List<Entry> selJQ() {
        return entryMapper.selJQ();
    }

    @Override
    public Integer upWJ(Entry entry) throws Exception {
        serviceManager.init();
        Map<String, CharityService> map = serviceManager.initCharityServiceManager();
        List<String> k = new ArrayList<>();
        for (String key : map.keySet()) {
            k.add(key);
        }
        charityService = map.get(k.get(0));
        charityService.init();
        CharityUpdateEntryTofafanInputBO updateEntryTofafanInputBO = new CharityUpdateEntryTofafanInputBO(entry.getId(), entry.getStateM(), entry.getReviewerM(), entry.getIssuedAmount());
//        charityService.updateEntryTofafan(updateEntryTofafanInputBO);
        return entryMapper.upWJ(entry.getId(), "已凑齐");
    }

    @Override
    public Integer upFF(Entry entry) throws Exception {
        serviceManager.init();
        Map<String, CharityService> map = serviceManager.initCharityServiceManager();
        List<String> k = new ArrayList<>();
        for (String key : map.keySet()) {
            k.add(key);
        }
        charityService = map.get(k.get(0));
        charityService.init();
        CharityUpdateEntryTofafanInputBO updateEntryTofafanInputBO = new CharityUpdateEntryTofafanInputBO(entry.getId(), entry.getStateM(), entry.getReviewerM(), entry.getIssuedAmount());
//        charityService.updateEntryTofafan(updateEntryTofafanInputBO);
        return entryMapper.upWJ(entry.getId(), entry.getStateM());
    }

    @Override
    public List<Entry> selFSQ() {
        return entryMapper.selFSQ();
    }

    @Override
    public String selAC(Long id) {
        return entryMapper.selAC(id);
    }

}
